module.exports = {
    // 日期格式化 format(date,'yyyy-MM-dd')
    format: (originVal, format) => {
        if (typeof originVal === "string") {
            var mts = originVal.match(/(\/Date\((\d+)\)\/)/);
            if (mts && mts.length >= 3) {
                originVal = parseInt(mts[2]);
            }
        }
        originVal = new Date(originVal);
        if (!originVal || originVal.toUTCString() == "Incalid Date") {
            return "";
        }

        var map = {
            M: originVal.getMonth() + 1, //月份
            d: originVal.getDate(), //日
            h: originVal.getHours(), //时
            m: originVal.getMinutes(), //分
            s: originVal.getSeconds(), //秒
            q: Math.floor((originVal.getMonth() + 3) / 3), //季度
            S: originVal.getMilliseconds(), //毫秒
        };

        format = format.replace(/([yMdhmsqS])+/g, function(all, t) {
            var v = map[t];
            if (v != undefined) {
                if (all.length > 1) {
                    v = "0" + v;
                    v = v.substr(v.length - 2);
                }
                return v;
            } else if (t === "y") {
                return (originVal.getFullYear() + "").substr(4 - all.length);
            }
            return all;
        });
        return format;
    },
    // 数组对象数据分组 dataGroup(arr, ['name', 'value']) 返回一个对象
    dataGroup: (dataSource, splitFields) => {
        // 把数据按照字段进行分组
        return splitFields.reduce((data, field) => {
            // 按照field过滤重复数据
            data[field] = dataSource.reduce((dataInner, item) => {
                dataInner[item[field]] = dataInner[item[field]];
                return dataInner;
            });
            return data;
        }, {});
    },
    // 防抖函数 getlist:debounce(function(){ 逻辑代码 },400)
    debounce: (fn, delay) => {
        // 记录上一次的延时器
        var timer = null;
        var delay = delay || 300;
        return function() {
            var args = arguments;
            var that = this;
            // 清除上一次延时器
            clearTimeout(timer);
            timer = setTimeout(function() {
                fn.apply(that, args);
            }, delay);
        };
    },
    // 对象复制
    cloneObject: obj => {
        return JSON.parse(JSON.stringify(obj));
    },
};